package com.sanyang.lawyer.controller.audit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sanyang.lawyer.ResultApi.CodeMsg;
import com.sanyang.lawyer.ResultApi.ResponseResult;
import com.sanyang.lawyer.entity.Audit;
import com.sanyang.lawyer.entity.Case_audit;
import com.sanyang.lawyer.entity.Case_law;
import com.sanyang.lawyer.entity.System_user;
import com.sanyang.lawyer.service.Audit.AuditService;
import com.sanyang.lawyer.service.Audit.Case_auditService;
import com.sanyang.lawyer.service.Case.Case_lawService;
import com.sanyang.lawyer.utils.ActivitiUtils;
import com.sanyang.lawyer.utils.RedisUtils;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("audit")
public class AuditController {
    
    @Autowired
    private ActivitiUtils activitiUtils;
    
    @Autowired
    private AuditService auditService;
    
    @Autowired
    private Case_auditService case_audit;
    
    @Autowired
    private RedisUtils redisUtils;
    
    @Autowired
    private Case_lawService lawService;
    private ObjectMapper objectMapper = new ObjectMapper();
    
    
    //立案审核
    @RequestMapping("startProcess")
    public ResponseResult startProcess(String data){
        Case_audit audit = null;
        try {
            audit = objectMapper.readValue(data, Case_audit.class);
        } catch (IOException e) {
            //错误返回服务器正忙错误
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        //开始一个流程并返回流程的id
        String processid = activitiUtils.start("委托代理人", "my05");
        //1：在流程和案件中间表添加数据
        case_audit.insertOne(processid,audit);
        //2：在流程表中添加数据
        auditService.insertOne(processid,audit);
        //3:将流程推行到下一步操作 进行经理级别的审核 5001
        activitiUtils.Mycomplete(processid);
        return ResponseResult.success("成功添加！~");
    }
    
    //根据流程id查询案件相关的内容
    @RequestMapping("queryCaseByProcessid")
    public ResponseResult<Case_law> queryCaseByProcessid(String processid){
        return ResponseResult.success(lawService.queryCaseByprocessid(processid));
    }
    
    //添加审核
    @RequestMapping("completeProcess")
    public ResponseResult completeProcess(String data){
        Audit audit = null;
        try {
            audit = objectMapper.readValue(data, Audit.class);
        } catch (IOException e) {
            //错误返回服务器正忙错误
            ResponseResult.error(CodeMsg.SERVER_BUSY);
        }
        //1：根据流程id将流程完成
        activitiUtils.Mycomplete(audit.getAuditid());
        //2：给流程表添加数据
        auditService.ComplateInsert(audit);
        //3: 判断是否通过，通过就将案件表的审核状态修改
        Case_law case_law = new Case_law();
        if(audit.getIf_pass().equals("通过")){
            case_law.setCloseState("通过");
            case_law.setCloseMode("已收案");
            case_law.setCloseDate(new Date());
        }else if(audit.getIf_pass().equals("不通过")){
            //如果不通过就将案件的审核状态修改为审核不通过
            case_law.setCloseState("不通过");
            case_law.setCloseMode("未收案");
            case_law.setCloseDate(new Date());
        }
        lawService.update_closestate(case_law);
        
        //4：将流程表的审核修改为已审核
        Audit newA = new Audit();
        newA.setState("已审核");
        auditService.update_state(newA);
        
        return ResponseResult.success("完成流程！~");
    }
    
    //根据登录用户查询这个用户的流程申请
    @RequestMapping("findApplyByUserid")
    public ResponseResult<List<Case_audit>> findApplyByUserid(String data){
        Case_audit audit = null;
        if(data!=null){
            try {
                audit = objectMapper.readValue(data, Case_audit.class);
            } catch (IOException e) {
                //错误返回服务器正忙错误
                ResponseResult.error(CodeMsg.SERVER_BUSY);
            }
        }
        System.out.println(audit.getAuditid());
        return ResponseResult.success(case_audit.findApplyByUserid(audit));
    }


    //根据流程id查询出流程的审批记录
    @RequestMapping("findProcessListByProcessid")
    public ResponseResult<List<Audit>> findProcessListByProcessid(String processid){
        return ResponseResult.success(auditService.findProcessListByProcessid(processid));
    }
    
    
            

    //根据当前登录用户查询可执行的任务
    @RequestMapping("findTaskByUserName")
    public ResponseResult<List<Case_audit>> findTaskByUserName(String userid){
        System.out.println(userid);
        System_user user = (System_user) redisUtils.get("user"+userid);
        List<Task> taskByUserName = activitiUtils.findTaskByUserName(user.getUserName());
        if(taskByUserName.size()==0 && taskByUserName==null){
            return ResponseResult.success(null);
        }
        return ResponseResult.success(case_audit.findAuditByTask(taskByUserName));
    }
}
